package com.example.administrator.shzs.ui;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.entity.Integral;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 积分兑换详情
 */
public class IntegraItemItemActivity extends BaseActivity implements View.OnClickListener {

    private TextView title;
    private TextView needIntergra;
    private TextView price;
    private TextView amout;
    private TextView intro;
    private ImageView ivPic;
    private Button btnExchange;
    private ImageView ivBack;

    //积分兑换列表的ID

    private Integral integral;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.integraxrecycle_item_item);
        MyApplication.app.activityList.add(this);
        //初始化
        inidata();

        //设置数据
        setData();

        //监听事件
        setListeners();

    }


    /**
     * 监听器
     */
    private void setListeners() {

        btnExchange.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }

    private void setData() {

        //获取礼品信息
        Intent intent = getIntent();
        integral = (Integral) intent.getSerializableExtra("Integral");
        if (integral.getPicture() != null) {
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.ic_default_adimage)
                    .showImageForEmptyUri(R.drawable.ic_default_adimage)
                    .showImageOnFail(R.drawable.ic_default_adimage)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();
            ImageLoader.getInstance().displayImage(integral.getPicture(),
                    ivPic, options);
        }
        title.setText(integral.getGname() == null ? "加载失败" : integral.getGname());
        needIntergra.setText(integral.getNeedIntegral() == null ? "加载失败" : integral.getNeedIntegral());
        price.setText(integral.getGiftsPrice() == null ? "加载失败" : integral.getGiftsPrice());
        amout.setText(integral.getStocks() == null ? "加载失败" : integral.getStocks());
        intro.setText(integral.getContent() == null ? "暂无信息" : "礼物介绍 :  " + integral.getContent());

    }

    private void inidata() {
        title = (TextView) findViewById(R.id.title);//标题
        needIntergra = (TextView) findViewById(R.id.intergra);//所需积分
        price = (TextView) findViewById(R.id.price);//价格
        amout = (TextView) findViewById(R.id.amout);//剩余数量
        intro = (TextView) findViewById(R.id.intro);//介绍
        ivPic = (ImageView) findViewById(R.id.integral_Image);//图片
        btnExchange = (Button) findViewById(R.id.btn_exchange);//兑换
        ivBack = (ImageView) findViewById(R.id.iv_Back_integraxrecycle_item_item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_exchange://兑换
                int currentPoint = 0;
                if (MyApplication.app.getUserId() != null ) {
                    //需要的积分
                    int intergra = Integer.parseInt(needIntergra.getText().toString().trim());
                    //现在账户积分
                    String point = MyApplication.app.getPoint();
                    if (point != null){
//                        转int
                        currentPoint = Integer.parseInt(point);
                    }
                    if (currentPoint >= intergra ) {
                        Intent inten = new Intent(this, IntegraEditActivity.class);
                        inten.putExtra("title", integral.getGname());
                        inten.putExtra("Intergra", integral.getNeedIntegral());
                        inten.putExtra("GiftsId", integral.getGiftsId());
                        startActivity(inten);
                    } else {
                        Toast.makeText(this, "亲，您的积分还差一点点", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "请登录之后进行此操作", Toast.LENGTH_SHORT).show();
                }

                break;

            case R.id.iv_Back_integraxrecycle_item_item://返回

                finish();

                break;
        }
    }
}
